#include"pch.h"
#include <graphics.h>
#include <conio.h>
#include <math.h>
#include<stdio.h>
#include<stdlib.h>
#include <windows.h>
#include <time.h>
#pragma comment(lib,"Winmm.lib")		// 引用 Windows Multimedia API

#define Width 950		//定义界面大小
#define High 640
//定义迷宫大小
#define zk_number_x 21 //迷宫的高度
#define zk_number_y 31 //迷宫的宽度

//	定义IMAGE对象
IMAGE img_bk1, img_hbk, img_pbk, img_lose, img_heart1, img_heart2, img_heart1H, img_heart2H, img_zk,img_HB1,img_HB2;	//	界面相关图像
IMAGE img_begin1, img_begin2, img_help1, img_help2, img_RecordFile;	//图标
IMAGE img_HM1, img_HM2, img_HM3, img_HM4, img_HM5, img_HM6, img_HM7, img_HM8;	//海绵宝宝行走相关图像
IMAGE img_HM_h1, img_HM_h2;			//海绵宝宝受伤
IMAGE img_SM1, img_SM2;		//敌人相关图像

int HMBB_x,HMBB_y;	// 定义海绵宝宝位置
int BK_x,BK_y;		//定义背景位置
int HMBB_Dir;		//记录海绵宝宝方向的变量
int Begin_x, Begin_y;	//"开始游戏"位置
int help_x, help_y;		//"?"位置
int RecordFile_x, RecordFile_y;	//"存档"位置
int heart_x, heart_y;	//记录生命的爱心的位置
int heart_num;			//记录爱心数目
int heart_hb;			//记录吃汉堡数目
int left_i=0, right_i=0, up_i=0, down_i=0;		//行走动画 每帧在图片上位置
int is_Meet;		//判断海绵宝宝和水母是否相撞
int SMn;              //红色水母数目
int HBn;				//汉堡包数目
int SMx[3], SMy[3];        //红色水母位置坐标
int HBx[4], HBy[4];			//汉堡位置坐标
int direction[3];            //水母移动方向
int map[zk_number_y + 2][zk_number_x + 2];

int gameStatus = 0; // 游戏状态，0为初始菜单界面，1为游戏说明，2为单人游戏，4为结束游戏状态

void startMenu();	// 初始菜单界面
void help();		//帮助界面
void Maze(int x, int y);	//随机生成迷宫函数
void startup();		// 数据初始化	
void show();		// 显示画面
void updateWithoutInput();  // 与用户输入无关的更新
void updateWithInput();     // 与用户输入有关的更新
void gameover();     // 游戏结束、后续处理

void startMenu()	 // 初始菜单界面
{
	putimage(BK_x, BK_y, &img_bk1);		//背景
	if (BK_y > -9250)			//使导入图片滑动
	{
		Sleep(20);
		BK_y = BK_y - 50;
	}
	else
	{
		putimage(Begin_x, Begin_y, &img_begin2, NOTSRCERASE);	//开始按键
		putimage(Begin_x, Begin_y, &img_begin1, SRCINVERT);
		putimage(help_x, help_y, &img_help2, NOTSRCERASE);	//?按键
		putimage(help_x, help_y, &img_help1, SRCINVERT);
		putimage(RecordFile_x, RecordFile_y, &img_RecordFile);	//读档按键
	}
	FlushBatchDraw();
	MOUSEMSG m;		// 定义鼠标消息
	if(MouseHit())  //这个函数用于检测当前是否有鼠标消息	
	{
		m = GetMouseMsg();
		if (m.uMsg == WM_LBUTTONDOWN || m.uMsg == WM_LBUTTONDBLCLK)
		{
			if ((m.x > help_x) && (m.x < help_x + 48) && (m.y > help_y) && (m.y < help_y + 48))	//判断是否点击“?”
			{
				mciSendString(L"open E:\\HMBB\\BGM\\help.mp3 alias fgmusic", NULL, 0, NULL); // 打开跳动音乐
				mciSendString(L"play fgmusic", NULL, 0, NULL); // 仅播放一次
				Sleep(50);
				gameStatus = 1;		//改变游戏状态
			}
			if ((m.x > Begin_x) && (m.x < Begin_x + 173) && (m.y > Begin_y) && (m.y < Begin_y + 90))	//判断是否点击“开始游戏”
			{
				mciSendString(L"close fgmusic", NULL, 0, NULL); // 关闭前一次的音乐
				mciSendString(L"open E:\\HMBB\\BGM\\begin.mp3 alias fgmusic", NULL, 0, NULL); // 打开跳动音乐
				mciSendString(L"play fgmusic", NULL, 0, NULL); // 仅播放一次
				Sleep(50);
				gameStatus = 2;		//改变游戏状态
			}	
		}
	}
}

void help()		//帮助界面
{
	putimage(0, 0, &img_hbk);
	setbkmode(TRANSPARENT);
	settextcolor(WHITE);
	settextstyle(30, 0, _T("黑体"));
	outtextxy(Width*0.1, High*0.35, _T("点击开始游戏选择游戏模式 点击√读取存档"));
	outtextxy(Width*0.1, High*0.4, _T("通过WASD控制海绵宝宝移动的方向"));
	outtextxy(Width*0.1, High*0.45, _T("蓝色水母会追击玩家 撞上水母生命减少一条"));
	outtextxy(Width*0.1, High*0.5, _T("♥表示生命 初始生命为三条 吃汉堡可以增加生命"));
	outtextxy(Width*0.1, High*0.55, _T("游戏过程中按ESC键暂停游戏 按任意键继续"));
	outtextxy(Width*0.1, High*0.6, _T("按空格键进入游戏界面 按ESC键回到初始界面 "));
	FlushBatchDraw();
	Sleep(2);

	char input;
	if (_kbhit())  // 判断是否有输入
	{
		input = _getch();  // 根据用户的不同输入来移动，不必输入回车
		if (input == ' ') 
		{
			mciSendString(L"close fgmusic", NULL, 0, NULL); // 关闭前一次的音乐
			mciSendString(L"open E:\\HMBB\\BGM\\begin.mp3 alias fgmusic", NULL, 0, NULL); // 打开跳动音乐
			mciSendString(L"play fgmusic", NULL, 0, NULL); // 仅播放一次
			Sleep(50);
			gameStatus = 2;
		}
		if (input == 27) // ESC键的ACSII码为27
		{
			gameStatus = 0;
		}
	}
}

void Maze(int x, int y) //随机生成迷宫
{
	int c[4][2] = { 0,1,1,0,0,-1,-1,0 }; //四个方向
	int i, j, t;
	//将方向打乱
	for (i = 0; i < 4; i++)
	{
		j = rand() % 4;
		t = c[i][0];
		c[i][0] = c[j][0];
		c[j][0] = t;
		t = c[i][1];
		c[i][1] = c[j][1];
		c[j][1] = t;
	}
	map[x][y] = 2;
	for (i = 0; i < 4; i++)
	{
		if (map[x + 2 * c[i][0]][y + 2 * c[i][1]] == 1)
		{
			map[x + c[i][0]][y + c[i][1]] = 2;
			Maze(x + 2 * c[i][0], y + 2 * c[i][1]);
		}
	}
}

//生成随机移动的水母
void creatSM1()
{
	for (SMn = 0; SMn < 3; SMn++)
	{
		int i, j;
		i = rand() % 29 + 2;
		j = rand() % 19 + 2;
		while (map[i][j] == 1 || map[i][j] == 3 || map[i][j] == 4 || map[i][j] == 5)//防止水母与墙壁、汉堡包、其他水母重合
		{
			i = rand() % 29 + 2;
			j = rand() % 19 + 2;
		}
		map[i][j] = 4;    //标记生成水母位置
		SMx[SMn] = 30 * i - 25;
		SMy[SMn] = 30 * j - 30;
	}
}
//随机生成汉堡包
void creatHB()
{
	int HBn = 4;
	for (HBn = 0; HBn < 4; HBn++)
	{
		int i, j;
		i = rand() % 29 + 2;
		j = rand() % 19 + 2;
		while (map[i][j] == 1 || map[i][j] == 3 || map[i][j] == 4 || map[i][j] == 5)//防止汉堡包与墙壁、水母、其他汉堡包重合
		{
			i = rand() % 29 + 2;
			j = rand() % 19 + 2;
		}
		map[i][j] = 5;    //标记生成汉堡包位置
		HBx[HBn] = 30 * i - 25;
		HBy[HBn] = 30 * j - 30;
	}
}


void startup()  //游戏初始化
{
	mciSendString(L"open E:\\HMBB\\BGM\\bgm.mp3 alias bkmusic", NULL, 0, NULL);//打开背景音乐
	mciSendString(L"play bkmusic repeat", NULL, 0, NULL);  // 循环播放
	initgraph(Width, High);
	loadimage(&img_bk1, L"E:\\HMBB\\PIC\\HMBBbackground1.jpg");		//导入图片
	loadimage(&img_lose, L"E:\\HMBB\\PIC\\GG.png"); 
	loadimage(&img_hbk, L"E:\\HMBB\\PIC\\help_BK1.jpg");
	loadimage(&img_pbk, L"E:\\HMBB\\PIC\\PBK.jpeg");
	loadimage(&img_HM1, L"E:\\HMBB\\PIC\\HMBBwalking1.png");
	loadimage(&img_HM2, L"E:\\HMBB\\PIC\\HMBBwalking2.png");
	loadimage(&img_HM3, L"E:\\HMBB\\PIC\\HMBBwalking3.png");
	loadimage(&img_HM4, L"E:\\HMBB\\PIC\\HMBBwalking4.png");
	loadimage(&img_HM5, L"E:\\HMBB\\PIC\\HMBBwalking5.png");
	loadimage(&img_HM6, L"E:\\HMBB\\PIC\\HMBBwalking6.png");
	loadimage(&img_HM7, L"E:\\HMBB\\PIC\\HMBBwalking7.png");
	loadimage(&img_HM8, L"E:\\HMBB\\PIC\\HMBBwalking8.png");
	loadimage(&img_HM_h1, L"E:\\HMBB\\PIC\\HMBBhurt1.png");
	loadimage(&img_HM_h2, L"E:\\HMBB\\PIC\\HMBBhurt2.png");
	loadimage(&img_SM1, L"E:\\HMBB\\PIC\\HMBBsm1.png");
	loadimage(&img_SM2, L"E:\\HMBB\\PIC\\HMBBsm2.png");
	loadimage(&img_HB1, L"E:\\HMBB\\PIC\\HMBB_hanbao1.png");
	loadimage(&img_HB2, L"E:\\HMBB\\PIC\\HMBB_hanbao2.png");
	loadimage(&img_begin1, L"E:\\HMBB\\PIC\\begin.png");
	loadimage(&img_begin2, L"E:\\HMBB\\PIC\\begin2.png");
	loadimage(&img_help1, L"E:\\HMBB\\PIC\\help1.png");
	loadimage(&img_help2, L"E:\\HMBB\\PIC\\help2.png");
	loadimage(&img_RecordFile, L"E:\\HMBB\\PIC\\存档.png");
	loadimage(&img_RecordFile, L"E:\\HMBB\\PIC\\存档.png");
	loadimage(&img_heart1, L"E:\\HMBB\\PIC\\xin1.png");
	loadimage(&img_heart2, L"E:\\HMBB\\PIC\\xin2.png");
	loadimage(&img_heart1H, L"E:\\HMBB\\PIC\\xin2.1.png");
	loadimage(&img_heart2H, L"E:\\HMBB\\PIC\\xin2.2.png");
	loadimage(&img_zk, L"E:\\HMBB\\PIC\\HMBBzhuankuai.jpg");
	HMBB_x = 5;
	HMBB_y = 30;
	BK_x = 0;
	BK_y = 0;
	heart_x = 5;
	heart_y = 0;
	Begin_x = Width / 4.5;
	Begin_y = 500;
	help_x = 885;
	help_y = 15;
	RecordFile_x= Width / 2;
	RecordFile_y=522;
	is_Meet = 0;
	HMBB_Dir = 0;
	heart_num = 3;
	heart_hb = 0;
	srand((unsigned)time(NULL)); //初始化随即种子
	BeginBatchDraw();
	while (gameStatus == 0)
	{
		startMenu(); // 初始菜单界面
	}
	while (gameStatus == 1)
	{
		help(); // 帮助界面
	}
	while (gameStatus == 0)
	{
		startMenu(); // 初始菜单界面
	}
	cleardevice();
	int i, j;
	for (i = 0; i <= zk_number_y + 1; i++)
	{
		for (j = 0; j <= zk_number_x + 1; j++)
		{
			if (i == 0 || i == zk_number_y + 1 || j == 0 || j == zk_number_x + 1) //初始化迷宫
				map[i][j] = 2;
			else
				map[i][j] = 1;
		}
	}
	Maze(2 * (rand() % (zk_number_y / 2) + 1), 2 * (rand() % (zk_number_x / 2) + 1)); //从随机一个点开始生成迷宫，该点行列都为偶数
	map[1][2] = 2; //给定入口
	map[zk_number_y][zk_number_x - 1] = 2; //给定出口
	for (i = 1; i <= zk_number_y; i++)
	{
		for (j = 1; j <= zk_number_x; j++) //画出迷宫
		{
			if (map[i][j] == 1)
				putimage(30 * (i - 1) + 5, 30 * (j - 1), &img_zk);
		}
		printf("\n");
	}
	putimage(HMBB_x, HMBB_y, 35, 39, &img_HM2, 0, 0, NOTSRCERASE);
	putimage(HMBB_x, HMBB_y, 35, 39, &img_HM1, 0, 0, SRCINVERT);
	for (int i = 0; i < 3; i++)//随机生成行进方向
	{
		direction[i] = rand() % 4 + 1;
	}
	creatSM1();
	creatHB();
	for (HBn = 0; HBn < 4; HBn++)
	{
		putimage(HBx[HBn], HBy[HBn], &img_HB2, NOTSRCERASE);
		putimage(HBx[HBn], HBy[HBn], &img_HB1, SRCINVERT);
	}
	BeginBatchDraw();
}

void Empty_data()		//初始化数据
{
	HMBB_x = 5;
	HMBB_y = 30;
	heart_x = 5;
	heart_y = 0;
	is_Meet = 0;
	heart_num = 3;
	cleardevice();
	int i, j;
	for (i = 0; i <= zk_number_y + 1; i++)
	{
		for (j = 0; j <= zk_number_x + 1; j++)
		{
			if (i == 0 || i == zk_number_y + 1 || j == 0 || j == zk_number_x + 1) //初始化迷宫
				map[i][j] = 2;
			else
				map[i][j] = 1;
		}
	}
	Maze(2 * (rand() % (zk_number_y / 2) + 1), 2 * (rand() % (zk_number_x / 2) + 1)); //从随机一个点开始生成迷宫，该点行列都为偶数
	map[1][2] = 2; //给定入口
	map[zk_number_y][zk_number_x - 1] = 2; //给定出口
	for (i = 1; i <= zk_number_y; i++)
	{
		for (j = 1; j <= zk_number_x; j++) //画出迷宫
		{
			if (map[i][j] == 1)
				putimage(30 * (i - 1) + 5, 30 * (j - 1), &img_zk);
		}
		printf("\n");
	}
	creatSM1();
	creatHB();
	for (HBn = 0; HBn < 4; HBn++)
	{
		putimage(HBx[HBn], HBy[HBn], &img_HB2, NOTSRCERASE);
		putimage(HBx[HBn], HBy[HBn], &img_HB1, SRCINVERT);
	}
	putimage(HMBB_x, HMBB_y, 35, 39, &img_HM2, 0, 0, NOTSRCERASE);
	putimage(HMBB_x, HMBB_y, 35, 39, &img_HM1, 0, 0, SRCINVERT);
}

void lose()				//输了后的界面
{
	putimage(0, 0, &img_lose);
	setbkmode(TRANSPARENT);
	settextcolor(RED);
	settextstyle(50, 0, _T("楷体"));
	outtextxy(Width*0.4, High*0.55, _T("重新开始"));
	outtextxy(Width*0.4, High*0.65, _T("退出游戏"));
	FlushBatchDraw();

	MOUSEMSG m;		// 定义鼠标消息
	if (MouseHit())  //这个函数用于检测当前是否有鼠标消息	
	{
		m = GetMouseMsg();
		if (m.uMsg == WM_LBUTTONDOWN || m.uMsg == WM_LBUTTONDBLCLK)
		{
			if ((m.x > Width*0.4) && (m.x < Width*0.4 + 200) && (m.y > High*0.55) && (m.y < High*0.55 + 50))	//判断是否点击“重新开始”
			{
				Empty_data();
				gameStatus = 2;		//改变游戏状态
			}
			if ((m.x > Width*0.4) && (m.x < Width*0.4 + 200) && (m.y > High*0.65) && (m.y < High*0.65 + 50))	//判断是否点击“退出游戏”
			{
				gameStatus = 4;		//改变游戏状态
				exit(0);
			}
		}
	}
}

void show()	//显示画面
{
	while (gameStatus == 4)
	{
		lose(); // 游戏结束界面
	}
	while (gameStatus == 0)
	{
		startMenu(); // 初始菜单界面
	}
	while (gameStatus == 1)
	{
		help(); // 帮助界面
	}
	if (heart_num +heart_hb <= 0)
	{
		gameStatus = 4;	//失败界面
	}
	putimage(heart_x, heart_y, 90, 27, &img_heart2, 0, 0, NOTSRCERASE);
	putimage(heart_x, heart_y, 90, 27, &img_heart1, 0, 0, SRCINVERT);
	if (heart_num + heart_hb == heart_num)
	{
		int i;
		for (i = 3; i > heart_num; i--)
		{
			putimage(heart_x + (i - 1) * 30, heart_y, 30, 30, &img_zk, 0, 0);
		}
	}
	else
	{
		if (heart_num == 3)
		{
			int i;
			for (i = 0; i < heart_hb; i++)
			{
				putimage(heart_x + (heart_num+i) * 30, heart_y, 30, 30, &img_heart2H, 0, 0, NOTSRCERASE);
				putimage(heart_x + (heart_num+i) * 30, heart_y, 30, 30, &img_heart1H, 0, 0, SRCINVERT);
			}
		}
		else if (heart_num < 3)
		{
			int i;
			for (i = 0; i < heart_hb; i++)
			{
				putimage(heart_x + (heart_num + i)* 30, heart_y, 30, 30, &img_heart2H, 0, 0, NOTSRCERASE);
				putimage(heart_x + (heart_num + i) * 30, heart_y, 30, 30, &img_heart1H, 0, 0, SRCINVERT);
			}
		}
	}
	for (SMn = 0; SMn < 3; SMn++)
	{
		putimage(SMx[SMn], SMy[SMn], 29.5, 36, &img_SM2, 0, 0, NOTSRCERASE);
		putimage(SMx[SMn], SMy[SMn], 29.5, 36, &img_SM1, 0, 0, SRCINVERT);
	}
	FlushBatchDraw();
	Sleep(50);
}

void updateWithoutInput()	//与用户输入无关的更新
{

	if (is_Meet == 0)		//红色水母自由移动
	{
		int i, j;
		int temp_direction[3];//记录行进方向
		for (SMn = 0; SMn < 3; SMn++)
		{
			i = (SMx[SMn] + 25) / 30;  //水母位置对应行列数
			j = (SMy[SMn] + 30) / 30;
			direction[SMn] = rand() % 4 + 1;
			if (direction[SMn] == 1 && (map[i - 1][j] == 2 || map[i - 1][j] == 4))
			{
				clearrectangle(SMx[SMn], SMy[SMn], SMx[SMn] + 30, SMy[SMn] + 30);
				SMx[SMn] = SMx[SMn] - 30;
			}
			else if (direction[SMn] == 2 && (map[i + 1][j] == 2 || map[i + 1][j] == 4))
			{
				clearrectangle(SMx[SMn], SMy[SMn], SMx[SMn] + 30, SMy[SMn] + 30);
				SMx[SMn] = SMx[SMn] + 30;
			}
			else if (direction[SMn] == 3 && (map[i][j - 1] == 2 || map[i][j - 1] == 4))
			{
				clearrectangle(SMx[SMn], SMy[SMn], SMx[SMn] + 30, SMy[SMn] + 30);
				SMy[SMn] = SMy[SMn] - 30;
			}
			else if (direction[SMn] == 4 && (map[i][j + 1] == 2 || map[i][j + 1] == 4))
			{
				clearrectangle(SMx[SMn], SMy[SMn], SMx[SMn] + 30, SMy[SMn] + 30);
				SMy[SMn] = SMy[SMn] + 30;
			}
			//当水母遇到汉堡，汉堡消失
			else if (direction[SMn] == 1 && map[i - 1][j] == 5)
			{
				clearrectangle(SMx[SMn], SMy[SMn], SMx[SMn] + 30, SMy[SMn] + 30);
				clearrectangle(SMx[SMn] - 30, SMy[SMn], SMx[SMn], SMy[SMn] + 30);
				SMx[SMn] = SMx[SMn] - 30;
			}
			else if (direction[SMn] == 2 && map[i + 1][j] == 5)
			{
				clearrectangle(SMx[SMn], SMy[SMn], SMx[SMn] + 30, SMy[SMn] + 30);
				clearrectangle(SMx[SMn], SMy[SMn] + 30, SMx[SMn] + 60, SMy[SMn] + 30);
				SMx[SMn] = SMx[SMn] + 30;
			}
			else if (direction[SMn] == 3 && map[i][j - 1] == 5)
			{
				clearrectangle(SMx[SMn], SMy[SMn], SMx[SMn] + 30, SMy[SMn] + 30);
				clearrectangle(SMx[SMn], SMy[SMn] - 30, SMx[SMn] + 30, SMy[SMn]);
				SMy[SMn] = SMy[SMn] - 30;
			}
			else if (direction[SMn] == 4 && map[i][j + 1] == 5)
			{
				clearrectangle(SMx[SMn], SMy[SMn], SMx[SMn] + 30, SMy[SMn] + 30);
				clearrectangle(SMx[SMn], SMy[SMn] + 30, SMx[SMn] + 30, SMy[SMn] + 60);
				SMy[SMn] = SMy[SMn] + 30;
			}
			else
			{
				direction[SMn] = rand() % 4 + 1;
			}
			Sleep(70);
		}
	}
	for (SMn = 0; SMn < 3; SMn++)
	{
		if (abs(HMBB_x - SMx[SMn]) + abs(HMBB_y - SMy[SMn]) < 30)
		{
			is_Meet = 1;
			heart_num--;
			mciSendString(L"close fgmusic", NULL, 0, NULL); // 关闭前一次的音乐
			int t;
			srand((int)time(NULL));
			t = rand() % 3 + 1;
			if (t == 1)
				mciSendString(L"open E:\\HMBB\\BGM\\电击1.mp3 alias fgmusic", NULL, 0, NULL); // 打开跳动音乐
			else if (t == 2)
				mciSendString(L"open E:\\HMBB\\BGM\\电击2.mp3 alias fgmusic", NULL, 0, NULL); // 打开跳动音乐
			else if (t == 3)
				mciSendString(L"open E:\\HMBB\\BGM\\电击3.mp3 alias fgmusic", NULL, 0, NULL); // 打开跳动音乐
			mciSendString(L"play fgmusic", NULL, 0, NULL); // 仅播放一次
			int i, j;
			for (i = 0; i < 4; i++)
			{
				putimage(SMx[SMn], SMy[SMn], 24.5, 30, &img_SM2, i*24.5, 0, NOTSRCERASE);
				putimage(SMx[SMn], SMy[SMn], 24.5, 30, &img_SM1, i*24.5, 0, SRCINVERT);
				FlushBatchDraw();
				Sleep(200);
			}
			for (j = 0; j < 3; j++)
			{
				clearrectangle(HMBB_x, HMBB_y, HMBB_x + 30, HMBB_y + 30);
				putimage(HMBB_x, HMBB_y, 40, 36, &img_HM_h2, j * 40, 0, NOTSRCERASE);
				putimage(HMBB_x, HMBB_y, 40, 36, &img_HM_h1, j * 40, 0, SRCINVERT);
				FlushBatchDraw();
				Sleep(200);
			}
			clearrectangle(SMx[SMn], SMy[SMn], SMx[SMn] + 30, SMy[SMn] + 30);
			if (SMy[SMn] < HMBB_y)
				SMy[SMn] = SMy[SMn] + 30;
			else if (SMy[SMn] > HMBB_y)
				SMy[SMn] = SMy[SMn] - 30;
			else if (SMx[SMn] < HMBB_x)
				SMx[SMn] = SMx[SMn] + 30;
			else if (SMx[SMn] > HMBB_x)
				SMx[SMn] = SMx[SMn] - 30;
			Sleep(40);
			if (heart_num+heart_hb > 0)
			{
				is_Meet = 0;
				clearrectangle(HMBB_x, HMBB_y, HMBB_x + 30, HMBB_y + 30);
				if (HMBB_Dir == 1)
				{
					putimage(HMBB_x, HMBB_y, 31.5, 30, &img_HM4, 0, 0, NOTSRCERASE);
					putimage(HMBB_x, HMBB_y, 31.5, 30, &img_HM3, 0, 0, SRCINVERT);
				}
				else if (HMBB_Dir == 2)
				{
					putimage(HMBB_x, HMBB_y, 31.5, 30, &img_HM2, 0, 0, NOTSRCERASE);
					putimage(HMBB_x, HMBB_y, 31.5, 30, &img_HM1, 0, 0, SRCINVERT);
				}
				else if (HMBB_Dir == 3)
				{
					putimage(HMBB_x, HMBB_y, 30, 30, &img_HM8, 0, 0, NOTSRCERASE);
					putimage(HMBB_x, HMBB_y, 30, 30, &img_HM7, 0, 0, SRCINVERT);
				}
				else if (HMBB_Dir == 4)
				{
					putimage(HMBB_x, HMBB_y, 30, 30, &img_HM6, 0, 0, NOTSRCERASE);
					putimage(HMBB_x, HMBB_y, 30, 30, &img_HM5, 0, 0, SRCINVERT);
				}
			}
		}
	}
	for (HBn = 0; HBn < 4; HBn++)
	{
		if (abs(HMBB_x - HBx[HBn]) + abs(HMBB_y - HBy[HBn]) < 30)
		{
			heart_hb++;
			mciSendString(L"close fgmusic", NULL, 0, NULL); // 关闭前一次的音乐
			mciSendString(L"open E:\\HMBB\\BGM\\eat.mp3 alias fgmusic", NULL, 0, NULL); // 打开跳动音乐
			mciSendString(L"play fgmusic", NULL, 0, NULL); // 仅播放一次
			clearrectangle(HBx[HBn], HBy[HBn], HBx[HBn] + 30, HBy[HBn] + 30);
			if (HMBB_Dir == 1)
			{
				putimage(HMBB_x, HMBB_y, 31.5, 30, &img_HM4, 0, 0, NOTSRCERASE);
				putimage(HMBB_x, HMBB_y, 31.5, 30, &img_HM3, 0, 0, SRCINVERT);
			}
			else if (HMBB_Dir == 2)
			{
				putimage(HMBB_x, HMBB_y, 31.5, 30, &img_HM2, 0, 0, NOTSRCERASE);
				putimage(HMBB_x, HMBB_y, 31.5, 30, &img_HM1, 0, 0, SRCINVERT);
			}
			else if (HMBB_Dir == 3)
			{
				putimage(HMBB_x, HMBB_y, 30, 30, &img_HM8, 0, 0, NOTSRCERASE);
				putimage(HMBB_x, HMBB_y, 30, 30, &img_HM7, 0, 0, SRCINVERT);
			}
			else if (HMBB_Dir == 4)
			{
				putimage(HMBB_x, HMBB_y, 30, 30, &img_HM6, 0, 0, NOTSRCERASE);
				putimage(HMBB_x, HMBB_y, 30, 30, &img_HM5, 0, 0, SRCINVERT);
			}
			Sleep(40);
		}
	}
	FlushBatchDraw();
	if (heart_num > 0)
	{
		is_Meet = 0;
	}
}

void updateWithInput()	//与用户输入有关的更新
{
	char input;
	int x = (HMBB_x - 5) / 30;
	int y = HMBB_y / 30;
	if (is_Meet == 0)
	{
		if (_kbhit())
		{
			input = _getch();
			int a = 0;
			if ((input == 'a'|| input == 'A') && (map[x][y + 1] != 1|| (map[x + 1][y + 1] !=1 && map[x][y + 1] == 1 && (HMBB_x - 5) % 30 > 0)) && HMBB_y % 30 == 0)
			{
				left_i++;
				HMBB_Dir = 1;
				clearrectangle(HMBB_x, HMBB_y, HMBB_x + 30, HMBB_y + 30);
				HMBB_x = HMBB_x - 10;
				putimage(HMBB_x, HMBB_y, 31.5, 30, &img_HM4, left_i * 31.5, 0, NOTSRCERASE);
				putimage(HMBB_x, HMBB_y, 31.5, 30, &img_HM3, left_i * 31.5, 0, SRCINVERT);
				FlushBatchDraw();
				Sleep(20);
				if (left_i == 8)
					left_i = 0;
				if (HMBB_x <= 10)		//防止超出边界；
					HMBB_x = 5;
			}
			if ((input == 'd'|| input == 'D')&&map[x + 2][y + 1]!=1 && HMBB_y % 30 == 0)
			{
				right_i++;
				HMBB_Dir = 2;
				clearrectangle(HMBB_x, HMBB_y, HMBB_x + 30, HMBB_y + 30);
				HMBB_x = HMBB_x + 10;
				putimage(HMBB_x, HMBB_y, 31.5, 30, &img_HM2, right_i * 31.5, 0, NOTSRCERASE);
				putimage(HMBB_x, HMBB_y, 31.5, 30, &img_HM1, right_i * 31.5, 0, SRCINVERT);
				FlushBatchDraw();
				Sleep(20);
				if (right_i == 8)
					right_i = 0;
				if (HMBB_x >= 900)		//防止超出边界；
					HMBB_x = 900;
			}
			if ((input == 'w'|| input == 'W') && (map[x + 1][y] !=1|| (map[x + 1][y + 1] !=1 && map[x + 1][y] == 1 && HMBB_y % 30 > 0)) && (HMBB_x - 5) % 30 == 0)
			{
				up_i++;
				HMBB_Dir = 3;
				clearrectangle(HMBB_x, HMBB_y, HMBB_x + 30, HMBB_y + 30);
				HMBB_y = HMBB_y - 10;
				putimage(HMBB_x, HMBB_y, 30, 30, &img_HM8, up_i * 30, 0, NOTSRCERASE);
				putimage(HMBB_x, HMBB_y, 30, 30, &img_HM7, up_i * 30, 0, SRCINVERT);
				FlushBatchDraw();
				Sleep(20);
				if (up_i == 2)
					up_i = 0;
				if (HMBB_y <= 10)		//防止超出边界；
					HMBB_y = 10;
			}
			if ((input == 's'|| input == 'S')&&map[x + 1][y + 2]!=1&& (HMBB_x - 5) % 30 == 0)
			{
				down_i++;
				HMBB_Dir = 4;
				clearrectangle(HMBB_x, HMBB_y, HMBB_x + 30, HMBB_y + 30);
				HMBB_y = HMBB_y + 10;
				putimage(HMBB_x, HMBB_y, 30, 30, &img_HM6, down_i * 30, 0, NOTSRCERASE);
				putimage(HMBB_x, HMBB_y, 30, 30, &img_HM5, down_i * 30, 0, SRCINVERT);
				FlushBatchDraw();
				Sleep(20);
				if (down_i == 3)
					down_i = 0;
				if (HMBB_y >= 590)		//防止超出边界；
					HMBB_y = 590;
			}
			if (input == 27) // ESC键的ACSII码为27
			{
				system("pause");
			}
		}
	}
}

void gameover()		//游戏结束
{
	EndBatchDraw();
	_getch();
	closegraph();
}

void main()
{
	startup();
	while (1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	}
	gameover();     // 游戏结束、后续处理
}
